package com.example.gtsp.hwdemo.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPool {
    private static final ScheduledExecutorService executor = new
            ScheduledThreadPoolExecutor(1, Executors.defaultThreadFactory());

    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        // 新建一个固定延迟时间的计划任务
        executor.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                if (haveMsgAtCurrentTime()) {
                    System.out.println(df.format(new Date()));
                    System.out.println("大家注意了，我要发消息了");
                }
            }
        }, 1, 1, TimeUnit.SECONDS);
    }

    public static boolean haveMsgAtCurrentTime() {
        //查询数据库，有没有当前时间需要发送的消息
        //这里省略实现，直接返回true
        return true;
    }

    /**
     * 最长回文串
     * @param strs
     * @return
     */
    public static int souLation(String strs) {
        if (strs.length() == 0) {
            return 0;
        }
        int count = 0;
        char[] chars = strs.toCharArray();
        HashSet<Character> hashset = new HashSet<>();
        for (int i = 0; i < chars.length; i++) {
            if (!hashset.contains(chars[i])) {
                hashset.add(chars[i]);
            } else {
                hashset.remove(chars[i]);
                count++;
            }
        }

        return hashset.isEmpty() ? count * 2 : count * 2 + 1;
    }
}
